package cn.lastcc.pwdmanage.core;

import android.app.Application;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.util.Log;

/**
 * 重写了 Android Application 对象，主要用于自定义初始化动作
 * 
 * @author Added by baidongyang 2013-6-23
 * @version 15.5.22.1
 *
 */
public class CustomApp extends Application {

	private static final String TAG = "CustomApp";

    private String userPwd;
    private int versionCode = 0;
    private String versionName = "";

    /**
     * 当前用户密码的 AES 密钥，用于加密内存中缓存的用户密码，每次登录会变
     */
    public String encpwd = "";
    // encpwd 的长度
    private static final int ENCPWDLEN = 20;

    /**
     * 获取全局变量：当前用户登陆密码
     * @return
     * @author Added by baidongyang 2013-6-23
     */
    public String getUserPwd() {
        return userPwd;
    }

    /**
     * 设置全局变量：当前用户登陆密码
     * @param userPwd
     * @author Added by baidongyang 2013-6-23
     */
    public void setUserPwd(String userPwd) {
    	// Modify by baidongyang 为缓存的用户密码加密 2015-05-22
    	PwdAction pwdAction = new PwdAction();
    	if ("".equals(this.encpwd)) {
	        this.encpwd = pwdAction.generatePwd(
	        		PwdAction.weight_lowLetters | PwdAction.weight_uppLetters
	        		| PwdAction.weight_nums | PwdAction.weight_symbolChars
	        		, ENCPWDLEN);
    	}
        byte [] encryptResult = AesUtil.encrypt(userPwd, this.encpwd);
        this.userPwd = AesUtil.parseByte2HexStr(encryptResult);
    }

    /**
     * 获取全局变量：代码版本号
     * @return
     * @author Added by baidongyang 2013-6-23
     */
    public int getVersionCode() {
        return versionCode;
    }

    /**
     * 设置全局变量：代码版本号
     * @param versionCode
     * @author Added by baidongyang 2013-6-23
     */
    public void setVersionCode(int versionCode) {
        this.versionCode = versionCode;
    }

    /**
     * 获取全局变量：显示版本号
     * @return
     * @author Added by baidongyang 2013-6-23
     */
    public String getVersionName() {
        return versionName;
    }

    /**
     * 设置全局变量：显示版本号
     * @param versionName
     * @author Added by baidongyang 2013-6-23
     */
    public void setVersionName(String versionName) {
        this.versionName = versionName;
    }

    /**
     * 获取全局变量：当前用户密码的 AES 密钥
     * @return
     * @author Added by baidongyang 2015-05-22
     */
    public String getEncpwd() {
		return encpwd;
	}

    /**
     * 设置全局变量：当前用户密码的 AES 密钥
     * @param encpwd
     * @author Added by baidongyang 2015-05-22
     */
	public void setEncpwd(String encpwd) {
		this.encpwd = encpwd;
	}

	@Override
    public void onCreate() {
        super.onCreate();
        /* 初始化全局变量 */
        PackageInfo packageInfo = null;;
        try {
            packageInfo = this.getPackageManager().getPackageInfo(super.getPackageName(), 0);
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
        Log.d("debug", String.valueOf(packageInfo));
        // 获得版本号
        if (null != packageInfo) {
            this.versionCode = packageInfo.versionCode;
            this.versionName = packageInfo.versionName;
        }
    }

}
